""" -*- coding: utf-8 -*-"""
# setup.py
import os
from app import create_app
from extensions import db
from models import Department, Code, User, ProjectType, DesignPhase, FileType

app = create_app()


def setup_database():
    """初始化数据库和基本数据"""
    with app.app_context():
        # 创建数据库表
        db.create_all()

        # 添加初始数据
        if not Department.query.first():
            # 添加部门
            dept1 = Department(name="设计一院", manager="张经理", phone="13800138000", address="A栋501")
            dept2 = Department(name="设计二院", manager="李经理", phone="13900139000", address="B栋302")

            # 添加编号
            code1 = Code(value="Y01")
            code2 = Code(value="Y02")
            dept1.codes.append(code1)
            dept2.codes.append(code2)

            db.session.add_all([dept1, dept2])
            db.session.commit()

            # 添加管理员
            admin = User(
                name="管理员",
                email="admin@example.com",
                department_id=dept1.id,
                role="admin"
            )
            admin.set_password("admin123")
            db.session.add(admin)
            db.session.commit()

            # 添加项目类型
            project_types = [
                ProjectType(name="建筑工程", description="建筑工程类项目"),
                ProjectType(name="市政工程", description="市政基础设施项目"),
                ProjectType(name="工业工程", description="工业厂房及设施项目")
            ]
            db.session.add_all(project_types)

            # 添加设计阶段
            design_phases = [
                DesignPhase(name="设计前期", description="项目设计前期阶段"),
                DesignPhase(name="方案设计", description="项目初步方案设计阶段"),
                DesignPhase(name="初设/施工图", description="项目初步设计、施工图设计阶段"),
                DesignPhase(name="后期服务", description="施工图设计阶段")
            ]
            db.session.add_all(design_phases)
            db.session.commit()

            # 添加文件类型
            file_types = [
                FileType(design_phase_id=design_phases[0].id, name="红线图", description="甲方xx人于xx年xx月xx日通过微信提供"),
                FileType(design_phase_id=design_phases[1].id, name="概念方案", description="概念成果汇报版本"),
                FileType(design_phase_id=design_phases[2].id, name="初设成果", description="初设成果，未上会")
            ]
            db.session.add_all(file_types)
            db.session.commit()

            print("Database initialized with sample data")
        else:
            print("Database already initialized")


if __name__ == '__main__':
    setup_database()